/*
	site/entry/ipad.jsp使用
	Created: 2012-04-13 sam.huang
*/

/*
 * ====================================================
 * 服务接口初始化
 */


function initPageParam(obj){
	PAGEATTR.nickname = obj.nickname;
	info("[initPageParam] nickname = " + PAGEATTR.nickname);
}
/*
 * ====================================================
 * 页面数据定义
 */
var PAGEATTR = {
		nickname: null
		// can add more attributes here as needed
	};

var VIDEO = {
		isPlaying: false,
		isStarted: false,
		baseCurrentTime:0,
		resetVideo:true
};

function resetVideo(){
	info("------ reset video --------");
	VIDEO.resetVideo = true;
}

var EVENT = {
		ppt: "EVENT_PPT_TURNING",
		qa: "EVENT_QA_APPEND",
		sysmsg: "EVENT_SYSMSG_APPEND"//系统消息是在QA装载数据中，为减少逻辑分支，使用EVENT.qa事件
	};

/*
 * ====================================================
 * 调度器
 */
$(function(){

});


/*
 * ====================================================
 * 视频
 */
$(function(){
	$("#video").bind('playing', onPlaying);
	$("#video").bind('ended', onEnded);
	$("#video").bind('pause', onPause);
	
	window.addEventListener("orientationchange", function() {
		fixFullscreen($(".fullScreen"));
		videoStartBtn();
	});
});

//开始播放时执行
function onPlaying(){
	info("[ipad-live][video] video playing!!");
	VIDEO.isPlaying = true;
	VIDEO.isStarted = true;
	Sch.start(videoPlayedTime);
	$("#videoStartBtn").hide();
	if(VIDEO.resetVideo){
		VIDEO.baseCurrentTime = $("#video")[0].currentTime;
		VIDEO.resetVideo = false;
		info("------update video's base currentTime---------", VIDEO.baseCurrentTime);
	}
	
	info("[ipad-live][video] video's currentTime when playing: "+$("#video")[0].currentTime+"s");
	info("[ipad-live][video] base currentTime:"+VIDEO.baseCurrentTime);
}

function requestAllPointTask(startTime, endTime){
	debug("[ipad-live][requestAllPointTask] startTime:"+startTime+",endTime"+endTime);
	while(startTime < endTime){
		Scheduler.requestPointTask(startTime * 1000);
		debug("[ipad-live][requestAllPointTask] -----:"+startTime);
		startTime += 0.2;
	}
}

//视频停止后执行
function onEnded(){
	info("[ipad-live][video] video ended!!");
	VIDEO.isPlaying = false;
	Sch.stop();
	$("#videoStartBtn").show();
}

//视频暂停后执行
function onPause(){
	info("[ipad-live][video] video pause!!");
	VIDEO.isPlaying = false;
	Sch.stop();
	$("#videoStartBtn").show();
}

//视频已播放时长，单位毫秒
function videoPlayedTime(){
	var s = 0;
	if(VIDEO.baseCurrentTime){
		s =  $("#video")[0].currentTime - VIDEO.baseCurrentTime;
	}else{
		s = $("#video")[0].currentTime;
	}
	return s*1000;
}

//放映的时间点，单位毫秒
function playingTime(timepoint, basetime){
	var resulttime = timepoint - basetime;
	return resulttime;
}

/*
 * ====================================================
 * 文档
 */
$(function(){
	Scheduler.addEventHandler(EVENT.ppt, pptHandler);//加载PPT的任务回调处理器
});

//装载一次PPT数据：
//{inittimestamp:inittimestamp, pageArray:[{hls:hsl,height:height,width:width,starttimestamp:starttimestamp,stoptimestamp:stoptimestamp}]}
function loadPPT(data){
	info("[ipad-live][loadPPT]------load ppt data------", data);
	var pptdatas = data.pageArray;
	for(i in pptdatas){
		appendPPT(data.inittimestamp, pptdatas[i]);
	}
}

//添加一个文档调度任务（对延迟装载的文档进行按需展示）
function appendPPT(inittimestamp, pptdata){
	pptdata.inittimestamp = inittimestamp;
	var startValue = playingTime(pptdata.starttimestamp, inittimestamp);
	var endValue = playingTime(pptdata.stoptimestamp, inittimestamp);
	if(endValue < startValue) endValue = startValue + 200*60*1000;
	Scheduler.addDurationTask(startValue, endValue, EVENT.ppt, pptdata);
}

//文档回调处理器（供调度器任务回调）
function pptHandler(data){
	$("#pptImg").attr("src", data.hls);
	
	//log
	group("group");
	info("video currentTime:", $("#video")[0].currentTime+"s");
	info("base currentTime:", VIDEO.baseCurrentTime+"s");
	info("ppt data:", data);
	info("ppt data's inittimestamp:", data.inittimestamp);
	groupEnd();
}

/*
 * ====================================================
 * 视窗大小适应，视窗交换，视窗全屏等用户接口（由触摸、滑动、手势等触发）
 */
$(function(){
	videoStartBtn();
	
	$("#videoBtn").bind("click", videoToMainView);
	$("#docBtn").bind("click", docToMainView);
	$("#qaBtn").bind("click", qaToMainView);
	
	bindPptFullscreenGesture();
	bindVideoFullscreenGesture();
	bindVideoTouchwipe();
	bindPptTouchwipe();
});

function mainViewIsDoc(){
	return $("#pptView").parent().is("#mainView");
};

function mainViewIsVideo(){
	return $("#videoView").is(".videoMove");
}

function enterFullscreen($obj, foo){
	debug("--- enter fullscreen");
	$obj.addClass("fullScreen");
}

function fixFullscreen($fullscreenElem){
	if(Nav.isOS5_0 && $fullscreenElem){
		if($fullscreenElem.is("#pptImgView")){
			window.scrollTo(0,0);
			return;
		}
		
		var cssObj;
		$fullscreenElem.attr("style","");
		var oldTop = $fullscreenElem.position().top;
		if(isLandscape()){
			cssObj = {top:(Number(window.pageYOffset)+Number(oldTop))+"px", height: (674 - Nav.browserDiffOffset)+"px"};
		}else{
			cssObj = {top:(Number(window.pageYOffset)+Number(oldTop))+"px", height: (928 - Nav.browserDiffOffset)+"px"};
		}
		$fullscreenElem.css(cssObj);
	}
}

function existFullscreen($obj){
	debug("--- exist fullscreen");
	$obj.removeClass("fullScreen");
	$obj.css({top:"", height:""});
}

//重新调整播放按钮位置
function videoStartBtn() {
	w=$("#videoWrap").width()/2;
	h=$("#videoWrap").height()/2;
	$("#videoStartBtn").css({left:w-34,top:h-34});
	debug("video start button position reset, left:"+(w-34)+",right:"+(h-34));
};

//请求将视频移位到主视窗
function videoToMainView(){
	if($("#videoView").is(".videoMove")) return; //已移位到主视窗
	
	moveVideo2mainView(); //视频移位
	moveEle("pptView", "rtView"); //文档加载到右上视窗
	if(!$("#qaView").parent().is("#rbView")){ //问答是否在右下视窗
		moveEle("qaView", "rbView"); //问答加载到右下视窗
	}
}

//请求将文档加载到主视窗
function docToMainView(){
	debug("--- doc to main view");
	if($("#pptView").parent().is("#mainView")) return;
	
	moveEle("pptView", "mainView");
	if($("#videoView").is(".videoMove")){
		moveVideo2rtView();
	}else if(!$("#qaView").parent().is("#rbView")){
		moveEle("qaView", "rbView");
	}
}

//请求将问答加载到主视窗
function qaToMainView(){
	if($("#qaView").parent().is("#mainView")) return;
	
	moveEle("qaView", "mainView");
	moveEle("pptView", "rbView");
	if($("#videoView").is(".videoMove")){
		moveVideo2rtView();
	}
}

//视频移位到主视窗
function moveVideo2mainView(){
	$("#videoView").addClass("videoMove");
	$("#videoWrap").removeClass("videoMin");
	$(".videoCntR").removeClass("videoCntRW");
	videoStartBtn();
}

//视频移位到右上视窗
function moveVideo2rtView(){
	$("#videoView").removeClass("videoMove");
	$("#videoWrap").addClass("videoMin");
	$(".videoCntR").addClass("videoCntRW");
	$("#videoWrap").css("position","");
	videoStartBtn();
}

//将元素移到模板元素内部，作为前置节点，并进行大小适应调整
function moveEle(eleId, destId){
	var ele = $("#"+eleId);
	var dest = $("#"+destId);
	dest.prepend(ele);
	if(destId=="mainView"){
		enlarge(eleId);
	}else{
		downsize(eleId);
	}
}

//放大（video通过移位完成，所有未包含视频逻辑）
function enlarge(eleId){
	if(eleId == "pptView"){
		$("#pptImgView").addClass("pptBigH");
		$("#pptImgView").removeClass("pptSmallH");
	}else if(eleId == "qaView"){
		$("#qaMyList").removeClass("askSmallH");
		$("#qaAllList").removeClass("askSmallH");
		$("#qaMyList").addClass("askBigH");
		$("#qaAllList").addClass("askBigH");
	}else{
		error("[ipad-live][enlarge] eleId 不合法："+eleId);
	}
}

//缩小（video通过移位完成，所以未包含视频逻辑）
function downsize(eleId){
	if(eleId == "pptView"){
		$("#pptImgView").removeClass("pptBigH");
		$("#pptImgView").addClass("pptSmallH");
	}else if(eleId == "qaView"){
		$("#qaMyList").removeClass("askBigH");
		$("#qaMyList").addClass("askSmallH");
		$("#qaAllList").removeClass("askBigH");
		$("#qaAllList").addClass("askSmallH");
	}else{
		error("[ipad-live][downsize] eleId 不合法："+eleId);
	}
}

/*
 * ====================================================
 * 提示框
 */
$(function(){
	initInfoDialog();
});

function initInfoDialog(){
	w2=$(".nipad").width()/2;
	h2=$(".nipad").height()/2;
	w3=$("#popDivId").width()/2;
	h3=$("#popDivId").height()/2;
	$("#popDivId").css({left:w2-w3,top:h2-h3});
	$("#winClose").click(function(){
			$("#popDivId").hide();
			$("#idCover").hide();
		});
}

/*
 * ====================================================
 * 调节日志级别
 */
$("#about").bind("gestureend", function(){
	if(event.scale > 1.2){
		if(LOG && LOG.level<4){
			LOG.level += 1;
		}
	}else if(event.scale < 0.8){
		if(LOG && LOG.level>0){
			LOG.level -= 1;
		}
	}
});

/*
 *!====================================================!
 */